package cartagows.wsframework.core;

import java.io.Serializable;

import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.llom.util.AXIOMUtil;

import cartago.CartagoException;

public class DataContainer implements Serializable{

	private static final long serialVersionUID = 1L;
	public static final int INTEGER = 1;
	public static final int STRING = 2;
	public static final int BOOLEAN = 3;
	public static final int OMELEMENT = 4;
	public static final int OPERATION_SPECIFIC = 5;

	private Object data;
	private int contentType;
	
	public DataContainer(int contentType, Object data) throws CartagoException{

		//Arguments check
		if (((data instanceof Integer) && (contentType==DataContainer.INTEGER))|| 
				((data instanceof String) && (contentType==DataContainer.STRING)) || 
				((data instanceof Boolean) && (contentType==DataContainer.BOOLEAN))|| 
				((data instanceof OMElement) && (contentType==DataContainer.OMELEMENT)) || 
				((data instanceof IOperationContent) && (contentType==DataContainer.OPERATION_SPECIFIC))){
			
			if ((data instanceof OMElement) && (contentType==DataContainer.OMELEMENT))			
				this.data = data.toString();
			else
				this.data = data;
			
			this.contentType = contentType;
		}
		//The data is not compatible with the contentType
		else
			throw new CartagoException();
	}
	
	public int getContentType(){
		return this.contentType;
	}
	
	public Object getData(){
		Object returnValue = null;
		if(contentType==OMELEMENT){
			try {
				returnValue = AXIOMUtil.stringToOM((String)data);
			} catch (XMLStreamException e) {
				e.printStackTrace();
			}
			return returnValue ;
		}
		else
			return data;
	}
}